import request from 'sync-request-curl';
import config from './config.json';

const port = config.port;
const url = config.url;

function registerAndGetToken() {
    const res = request(
        'POST',
        `${url}:${port}/v1/admin/auth/register`,
        {
            json: {
                email: 'hayden.smith@unsw.edu.au',
                password: 'haydensmith123',
                nameFirst: 'Hayden',
                nameLast: 'Smith'
            },
            timeout: 100
        }
    );

    const bodyObj = JSON.parse(res.body as string);
    return bodyObj.token;
}

test('注册', () => {
    const token = registerAndGetToken();

    const res = request(
        'POST',
        `${url}:${port}/v1/admin/auth/logout`,
        {
            json: { token: token },
            timeout: 100
        }
    );

    const bodyObj = JSON.parse(res.body as string);
    // expect(bodyObj).toEqual({}); // 成功登出应该返回空对象
});


function loginAndGetToken() {
    const loginResponse = request(
        'POST',
        `${url}:${port}/v1/admin/auth/login`,
        {
            json: {
                email: 'hayden.smith@unsw.edu.au',
                password: 'haydensmith123'
            },
            timeout: 100
        }
    );
    const loginBody = JSON.parse(loginResponse.body as string);
    return loginBody.token;
}

test('管理员退出成功', () => {
    const token = loginAndGetToken();
    const logoutResponse = request(
        'POST',
        `${url}:${port}/v1/admin/auth/logout`,
        {
            json: { token: token },
            timeout: 100
        }
    );

    const logoutBody = JSON.parse(logoutResponse.body as string);
    expect(logoutBody).toEqual({});
});

test('管理员退出失败测试', () => {
    const token = loginAndGetToken();
    const logoutResponse = request(
        'POST',
        `${url}:${port}/v1/admin/auth/logout`,
        {
            json: { token: "321312" },
            timeout: 100
        }
    );

    const logoutBody = JSON.parse(logoutResponse.body as string);
    expect(logoutBody).toEqual({
        "error": "Token is empty or invalid (does not refer to valid logged in user quiz session)"
      });
});





    // test('register successfully', () => {
    //     const res = request(
    //         'POST',
    //             `${url}:${port}/v1/admin/auth/register`,
    //             {
    //                 json: {
    //                     email: 'hydenht.smith@unsw.edu.au',
    //                     password: 'haydensmith123',
    //                     nameFirst: 'Hayden',
    //                     nameLast: 'Smith'
    //                 },
    //                 timeout: 100
    //             }
    //     );
    //     const bodyObj = JSON.parse(res.body as string);
    //     expect(bodyObj).toEqual(expect.objectContaining({
    //         token: expect.any(String)
    //     })); 

    // });

//     test('Fail to register with an email address used by another user', () => {
//         const res = request(
//             'POST',
//                 `${url}:${port}/v1/admin/auth/register`,
//                 {
//                     json: {
//                         email: 'haydenht.smith@unsw.edu.au',
//                         password: 'haydensmith123',
//                         nameFirst: 'Hayden',
//                         nameLast: 'Smith'
//                     },
//                     timeout: 100
//                 }
//         );
//         expect(res.statusCode).toBe(400);
//         const bodyObj = JSON.parse(res.body as string);
//         expect(bodyObj.error).toStrictEqual(expect.any(String)); 
//     });
    
//     test('Fail to register with invalid email', () => {
//         const res = request(
//             'POST',
//                 `${url}:${port}/v1/admin/auth/register`,
//                 {
//                     json: {
//                         email: 'john.doe@web',
//                         password: 'haydensmith123',
//                         nameFirst: 'Hayden',
//                         nameLast: 'Smith'
//                     },
//                     timeout: 100
//                 }
//         );
//         expect(res.statusCode).toBe(400);
//         const bodyObj = JSON.parse(res.body as string);
//         expect(bodyObj.error).toStrictEqual(expect.any(String));  
//     });
// });
// test('check user detail successfully', () => {
//     const res = request(
//         'GET',
//             `${url}:${port}/v1/admin/user/details?token=awe23`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual(expect.objectContaining({
//         user: {
//             userId: expect.any(Number),
//             name: expect.any(String),
//             email: expect.any(String),
//             numSuccessfulLogins: expect.any(Number),
//             numFailedPasswordsSinceLastLogin: expect.any(Number)
//         }
//     }));
// });

// test('check user detail successfully', () => {
//     const res = request(
//         'GET',
//             `${url}:${port}/v1/admin/user/details?token=awe23`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual(expect.objectContaining({
//         user: {
//             userId: expect.any(Number),
//             name: expect.any(String),
//             email: expect.any(String),
//             numSuccessfulLogins: expect.any(Number),
//             numFailedPasswordsSinceLastLogin: expect.any(Number)
//         }
//     }));
// });

// test('check author logout successfully', () => {
//     const res = request(
//         'POST',
//             `${url}:${port}/v1/admin/auth/logout?token=awe23`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual({});
// });

// test('check author logout unsuccessfully', () => {
//     const res = request(
//         'POST',
//             `${url}:${port}/v1/admin/auth/logout?token=aw88`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual({ error: 'Token is empty or invalid (does not refer to valid logged in user session)'});
// });

// test('check author view trash unsuccessfully', () => {
//     const res = request(
//         'GET',
//             `${url}:${port}/v1/admin/quiz/trash?token=9`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual({ error: 'Token is empty or invalid (does not refer to valid logged in user session)'});
// });

// test('check author view trash successfully', () => {
//     const res = request(
//         'GET',
//             `${url}:${port}/v1/admin/quiz/trash?token=awe23`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual({ error: 'Token is empty or invalid (does not refer to valid logged in user session)'});
// });

// test('check trash restore', () => {
//     const res = request(
//         'POST',
//             `${url}:${port}/v1/admin/:quizid/restore?token=99`,
//             {
//                 timeout: 100
//             }
//     );

//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual({ error: 'Token is empty or invalid (does not refer to valid logged in user session)'});
// });


// test('restore trash successfully', () => {
//     const res = request(
//         'POST',
//             `${url}:${port}/v1/admin/quiz/1/restore`,
//             {
//                 json: {
//                     token: 'qqq',
//                 },
//                 timeout: 100
//             }
//     );
//     const bodyObj = JSON.parse(res.body as string);
//     expect(bodyObj).toEqual(expect.objectContaining({
//         token: expect.any(String)
//     })); 

// });